home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / BLT2_205.ZIP / src / blt2demo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-25  |  12.0 KB  |  368 lines

  1. /*
  2.  *
  3.  * blt2demo.c - 17-Oct-1995 Cornel Huth
  4.  * This module calls blt2cx0?.c
  5.  * Bullet 2 Demonstration Program
  6.  * 25-Feb-96:
  7.  */
  8.  
  9. #define PRG_DATE "[25-Feb-96]"
  10.  
  11. #include "platform.h"           // defines platform (OS/2 or WIN32 or DOSX)
  12.  
  13. #ifdef ON_OS2
  14.    #define INCL_DOSPROCESS      // for OS/2 threads
  15.    #define INCL_DOSMISC         // for DosError()
  16.    #include <os2.h>
  17.    #define HANDLES_WANTED  1030 // can do 1024 Bullet files (3 for OS, ...)
  18. #endif
  19. #ifdef ON_W95
  20.    #define WIN32_LEAN_AND_MEAN
  21.    #include <windows.h>
  22.    #define HANDLES_WANTED  1030 // can do about 230 on VFAT (3 (or 5) for OS, ...)
  23.    // Bullet95.DLL can open and use up to 100 files at the same time, with up
  24.    // to two concurrent processes.
  25. #endif
  26. #ifdef ON_DOSX
  27.    #define HANDLES_WANTED   255 // can do 250 Bullet files (5 for OS)
  28. #endif
  29.  
  30. #ifndef ON_OS2
  31. #ifndef ON_W95
  32. #ifndef ON_DOSX
  33.    #error No ON_platform specified  // multiple defs not checked, so don't!
  34. #endif
  35. #endif
  36. #endif
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <time.h>
  41. #include <string.h>
  42.  
  43. #ifdef ON_OS2
  44.    #include "bullet2.h"
  45. #endif
  46. #ifdef ON_W95
  47.    #include "bullet95.h"
  48. #endif
  49. #ifdef ON_DOSX
  50.    #include <dos.h>             // for _harderr()
  51.    #include "bulletx.h"
  52. #endif
  53.  
  54. int cx01();
  55. int cx02();
  56. int cx03();
  57. int cx04();
  58. int cx05();
  59. int cx06();
  60. int cx07();
  61. int cx08();
  62. int cx09();
  63.  
  64.  
  65. // For the DOS-Extender version only:
  66. //
  67. // Memory is taken from the compiler/extender facility to ensure flatitude.
  68. // malloc() and free() are used (available in C or C++), but these can be
  69. // replaced if you are not using a C or C++ compiler.  The routines are
  70. // standard C functions, with the single, dword-sized parameter on the stack.
  71. // The parameter is also directly available in the EAX register (use either).  
  72. //
  73. // _BULLETMALLOC() receives the bytes requested, and is to return a pointer 
  74. // (FLAT) to the base of the allocation (the return is always in EAX -- this
  75. // is automatically done by all compilers).  If bytes== -1, then the amount
  76. // of memory available should be returned.  This value is currently used only
  77. // by MEMORY_XB, and is for informational use only.  A dummy value may be
  78. // returned, such as 65536 (or 524288, etc.).
  79. //
  80. // _BULLETFREE() receives the same pointer when the memory is no longer needed, 
  81. // and is to release the allocation.  Nothing need be returned, but if anything
  82. // must, return 0 (in EAX).
  83. //
  84. // _BULLETGETENV() is to return a pointer to 'TMP=' in the environment.  This
  85. // is for the temporary file created during REINDEX_XB.  If the current dir
  86. // is to be used instead, return NULL. 
  87. //
  88. // The three routines must be externally available.
  89.  
  90. #ifdef ON_DOSX
  91.  
  92.    // Bullet/X requires memory to be gotten from the run-time library 
  93.    // (for flatitude).
  94.    // Names must be _BULLETMALLOC, _BULLETFREE, and _BULLETGETENV.
  95.  
  96.    // WATCOM reg-based parameter passing uses FuncName_ as naming convention, 
  97.    // so if WATCOM, and reg-based parms, use the following #pragma
  98.  
  99.    #pragma aux BULLETMALLOC "_*";
  100.    #pragma aux BULLETFREE "_*";
  101.    #pragma aux BULLETGETENV "_*";
  102.  
  103.    ULONG BULLETMALLOC(ULONG bytes) {
  104.       if (bytes != 0xFFFFFFFF)
  105.          return (ULONG) malloc(bytes);
  106.          // cast and be happy
  107.       else
  108.          return (512*1024); 
  109.          // this return value is informational only, used only by MEMORY_XB
  110.    }
  111.  
  112.    ULONG BULLETFREE(VOID *ptr) {
  113.       free(ptr);
  114.       return 0;
  115.    }
  116.  
  117.    PVOID BULLETGETENV(void) {
  118.       return getenv("TMP");
  119.    }
  120.  
  121.  
  122.    // Also for DOS extenders, the critical error handler must be setup by your
  123.    // compiler, since, depending on it, and the extender, different methods are
  124.    // used to make it work.  This makes use of no Bullet routine, but it does
  125.    // access an global Bullet variable called _BULLETINT24.  This variable is
  126.    // set to 0xFFFF0000 at the start of a Bullet disk I/O access and set to 0
  127.    // after the access.  Therefore, you can tell if a critical error originates
  128.    // from a Bullet call.  The critical error handler is to set the low word of
  129.    // _BULLETINT24 with the error (actually, error + 19) and is to return an 
  130.    // 'ignore' flag back to the caller (ignore=0).  The INT24HANDLER() below 
  131.    // should make it obvious what needs to be done.
  132.    // This is for the DOSX32 version only.
  133.  
  134.    extern ULONG BULLETINT24;       // known as _BULLETINT24 to Bullet
  135.  
  136.    int __far INT24HANDLER(unsigned deverr,unsigned errcode,unsigned far *devhdr) {
  137.  
  138.       if ((!(deverr & 0x8000)) & ((BULLETINT24 & 0xFFFF0000) == 0xFFFF0000)) {
  139.  
  140.          // Bit15=0 is disk error, and:
  141.          // if disk error, and if high-word of BULLETINT24 is FFFF, then
  142.          // this critical error occured while processing a Bullet initiated
  143.          // disk access -- any other case then it is not from Bullet (such
  144.          // as file I/O you perform other than through Bullet).  Generally,
  145.          // DOS attempts/retries the operation 3 times (and enters this handler
  146.          // on each), and only after the third does it return back to Bullet.
  147.          // BULLETINT24's high word must be cleared on return.
  148.  
  149.          BULLETINT24 = errcode + 0x13;  // +19 to get standard DOS error code
  150.          return 0;                      // 0=_HARDERR_IGNORE
  151.       }
  152.  
  153.       // you may prefer to abort, rather than ignore, if not a Bullet error
  154.       // see your compiler for details
  155.  
  156.       // #include <conio.h>           // for cprintf()
  157.       // cprintf( "Critical error other than from Bullet: " ); 
  158.       // cprintf( "deverr=%4.4X errcode=%d\r\n",deverr, errcode ); 
  159.       // cprintf( "devhdr = %Fp\r\n", devhdr ); 
  160.  
  161.       return 0;
  162.    }
  163.  
  164. #endif
  165.  
  166. // Also for DOS-Extender version only (details in BULLETX.H)
  167. // (since some extenders may not support INT21/6501 and 6506...)
  168. //
  169. // Note that Windows does not use the standard code page tables that
  170. // OS/2 and DOS (DOSX) use -- for the most part, they are functionally
  171. // equal, however, they are not exactly the same -- compare as required
  172. // and use your own collate table if needed.
  173.  
  174. #ifdef INT2165xxNS              // this would be defined in BULLETX.H 
  175.                                 // table is global data, for code page 437
  176.    CHAR ctable[] = {
  177. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
  178. 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
  179. 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,
  180. 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
  181. 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,
  182. 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,
  183. 0x60,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,
  184. 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x7B,0x7C,0x7D,0x7E,0x7F,
  185. 0x43,0x55,0x45,0x41,0x41,0x41,0x41,0x43,0x45,0x45,0x45,0x49,0x49,0x49,0x41,0x41,
  186. 0x45,0x41,0x41,0x4F,0x4F,0x4F,0x55,0x55,0x59,0x4F,0x55,0x24,0x24,0x24,0x24,0x24,
  187. 0x41,0x49,0x4F,0x55,0x4E,0x4E,0xA6,0xA7,0x3F,0xA9,0xAA,0xAB,0xAC,0x21,0x22,0x22,
  188. 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,
  189. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,
  190. 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF,
  191. 0xE0,0x53,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,
  192. 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF
  193. };
  194.  
  195.    CHAR *collateTable = &ctable;
  196.  
  197. #else
  198.  
  199.    CHAR *collateTable = NULL;
  200.  
  201. #endif
  202.  
  203.  
  204.  
  205. // Main program start ////////////////////////////////////////////////////////
  206.  
  207. int main(void) {
  208.  
  209. #pragma pack(1)
  210.  
  211.    EXITPACK EP;
  212.    INITPACK IP;
  213.  
  214. #pragma pack()
  215.  
  216. int rez;
  217.  
  218. CHAR tmpStr[1024];
  219. CHAR aPRGDATE[] = PRG_DATE;
  220. CHAR op1[] = "Import/Add/REINDEX, with import data generated on-the-fly to DBF.";
  221. CHAR op2[] = "Key I/O on non-Bullet data file (only index file is maintained).";
  222. CHAR op3[] = "Atomic INSERT (transaction-list of up to 512 files).";
  223. CHAR op4[] = "Atomic UPDATE (transaction-list of up to 512 files).";
  224. CHAR op5[] = "Two-table joined view of EMPLOYEE and DEPARTMENT tables.";
  225. CHAR op6[] = "Memo support for text/BLOB data to DBT.";
  226. CHAR op7[] = "Files blowout -- many files opened simultaneously.";
  227. CHAR op8[] = "Lock file, region, & record; exclusive/shared relock.";
  228. CHAR op9[] = "Import/Add/REINDEX (as #1) using Custom sortFunction (float key).";
  229.  
  230. #ifdef USE_ANSI
  231.    CHAR aCLS[] = "\x1B[2J";
  232.    CHAR aGRN[] = "\x1B[1;32m";
  233.    CHAR aWHT[] = "\x1B[0;37m";
  234. #else
  235.    CHAR aCLS[] = "";
  236.    CHAR aGRN[] = "";
  237.    CHAR aWHT[] = "";
  238. #endif
  239.  
  240. setbuf(stdout,NULL);
  241.  
  242.  
  243. IP.func = INIT_XB;
  244. IP.JFTsize = HANDLES_WANTED;
  245.  
  246. rez = BULLET(&IP);
  247. if (rez) {
  248.    printf("Failed Bullet initialization.  Err: %li\n",rez);
  249.    goto ExitNow;
  250. }
  251.  
  252. #ifdef ON_OS2
  253.  
  254.    // Instead of using _hard() as is done for DOSX, in OS/2 all one needs to
  255.    // do to prevent pop-ups is to call DosError(FERR_DISABLEHARDERR) (or 0).
  256.    // You may prefer to place it around Bullet sections only (use
  257.    // FERR_ENABLEHARDERR, or 1, to restore pop-ups).  This does not disable 
  258.    // numeric or other exceptions, which would be FERR_DISABLEEXCEPTION, or 2.
  259.  
  260.    DosError(FERR_DISABLEHARDERR);
  261.  
  262. #endif
  263. #ifdef ON_W95
  264.  
  265.    // register EXIT_XB with the startup code's shutdown list so that Bullet
  266.    // is exited with files closed, etc.
  267.  
  268.    rez = atexit(IP.exitPtr);
  269.    if (rez)
  270.       printf("\a\n<*> Could not register EXIT_XB with atexit().  Continuing...\n");
  271.  
  272.    // critical errors back to program; prevent Windows from popping msg box
  273.  
  274.    SetErrorMode(SEM_FAILCRITICALERRORS);
  275.  
  276. #endif
  277. #ifdef ON_DOSX
  278.  
  279.    // register EXIT_XB with the startup code's shutdown list so that Bullet
  280.    // is exited with files closed, etc.
  281.  
  282.    rez = atexit(IP.exitPtr);
  283.    if (rez)
  284.       printf("\a\n<*> Could not register EXIT_XB with atexit().  Continuing...\n");
  285.  
  286.    // DOS extenders do not have a standard method of doing the critical error 
  287.    // handler.  Only your compiler knows (or you, the programmer) how the
  288.    // extender expects things to be set up.  Therefore, this is done at the
  289.    // program level, and is to set a Bullet variable (_BULLETINT24:DWORD) to
  290.    // the error code.  The low-word (16 bits) is all that is used.  This 
  291.    // handler comes into play when, for example, you attempt to open a file 
  292.    // on a floppy with no disk in, etc.).
  293.  
  294.    _harderr(INT24HANDLER);
  295.  
  296. #endif
  297.  
  298. while (1) {
  299.  
  300.    printf("%s%sBullet 2 Demo.%s  NLS IX3/DBF/DBT/external %s\n\n",aCLS,aGRN,aWHT,aPRGDATE);
  301.    printf("Take your pick (each has selectable run options):\n");
  302.    printf("\n 0. Exit demo\n");
  303.    printf(" 1. %s\n",op1);
  304.    printf(" 2. %s\n",op2);
  305.    printf(" 3. %s\n",op3);
  306.    printf(" 4. %s\n",op4);
  307.    printf(" 5. %s\n",op5);
  308.    printf(" 6. %s\n",op6);
  309.    printf(" 7. %s\n",op7);
  310.    printf(" 8. %s\n",op8);
  311.    printf(" 9. %s\n",op9);
  312.    printf("\nSelection: ");
  313.  
  314.    gets(tmpStr);
  315.    switch(*tmpStr) {
  316.    case '1':
  317.       printf("\n%s%s%s\n",aGRN,op1,aWHT);
  318.       rez = cx01();
  319.       break;
  320.    case '2':
  321.       printf("\n%s%s%s\n",aGRN,op2,aWHT);
  322.       rez = cx02();
  323.       break;
  324.    case '3':
  325.       printf("\n%s%s%s\n",aGRN,op3,aWHT);
  326.       rez = cx03();
  327.       break;
  328.    case '4':
  329.       printf("\n%s%s%s\n",aGRN,op4,aWHT);
  330.       rez = cx04();
  331.       break;
  332.    case '5':
  333.       printf("\n%s%s%s\n",aGRN,op5,aWHT);
  334.       rez = cx05();
  335.       break;
  336.    case '6':
  337.       printf("\n%s%s%s\n",aGRN,op6,aWHT);
  338.       rez = cx06();
  339.       break;
  340.    case '7':
  341.       printf("\n%s%s%s\n",aGRN,op7,aWHT);
  342.       rez = cx07();
  343.       break;
  344.    case '8':
  345.       printf("\n%s%s%s\n",aGRN,op8,aWHT);
  346.       rez = cx08();
  347.       break;
  348.    case '9':
  349.       printf("\n%s%s%s\n",aGRN,op9,aWHT);
  350.       rez = cx09();
  351.       break;
  352.    case '0':
  353.       break;
  354.    }
  355.    if ((*tmpStr=='0') | (rez))
  356.       break;
  357.  
  358.    printf("\nPress ENTER for select menu...");
  359.    getchar();
  360. }
  361. EP.func = EXIT_XB;
  362. rez = BULLET(&EP);
  363.  
  364. ExitNow:
  365. return rez;
  366. }
  367.  
  368.